package com.example.demoexcel;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelWriterTest03BigData {
    public static void main(String[] args) throws IOException {
        // 开始时间
        long start = System.currentTimeMillis();
        String path = "D:\\data\\";
        // 1.创建一个工作簿。03
        Workbook workbook = new SXSSFWorkbook(5000);
        // 2.创建一个工作表
        Sheet sheet = workbook.createSheet("统计表");
        // 3.创建行。
        for (int rowNum = 0; rowNum < 65537; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(rowNum + "," + cellNum);
            }
        }
        // 5.生成一张表。03版本的工作簿是以.xlsx结尾
        FileOutputStream fileOutputStream = new FileOutputStream(path + "07BigDataUpGrade.xlsx");
        // 输出
        workbook.write(fileOutputStream);
        // 6.关闭流
        fileOutputStream.close();
        // 7.清除临时文件
        ((SXSSFWorkbook) workbook).dispose();
        System.out.println("07大数据量表优化后生成成功！");
        // 结束时间
        long end = System.currentTimeMillis();
        System.out.println("用时：" + ((end - start) / 1000) + "秒");
    }
}
